<?php

namespace app\vpn\admin;

use app\common\builder\ZBuilder;
use app\user\model\User as UserModel;
use app\vpn\model\Signed;
use app\vpn\model\Node;
use app\vpn\model\Flow;

/**
 * 分析控制器
 */
class Report extends Index
{

    protected function initialize()
    {
        parent::initialize();
        if ($this->no_auth) {
            $this->error('权限不足！');
        }
        if (in_array(request()->action(), ['delete'])) {
            if (!$this->delete_safe) {
                $this->error('NO');
            }
        }
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $all_user = UserModel::where([])->count();

        $hour1 = UserModel::where('last_login_time', '>', strtotime('-1 hour'))->count();

        $hour24 = UserModel::where('last_login_time', '>', strtotime('-24 hour'))->count();

        $today = UserModel::where('last_login_time', '>', strtotime(date('Y-m-d')))->count();

        $over24 = $all_user - $hour24;

        $this->assign('all_user', $all_user);
        $this->assign('hour1', $hour1);
        $this->assign('hour24', $hour24);
        $this->assign('today', $today);
        $this->assign('over24', $over24);
        return $this->fetch();
    }

    /**
     * 签到
     * @return mixed
     */
    public function signed()
    {
        $all_user = UserModel::where([])->count();

        $signed_user = Signed::distinct(true)->field('uid')->select();
        $signed_user = count($signed_user);

        $today = Signed::where('create_time', '>', strtotime(date('Y-m-d')))->count();

        $over = $all_user - $signed_user;

        $this->assign('all_user', $all_user);
        $this->assign('signed_user', $signed_user);
        $this->assign('today', $today);
        $this->assign('over', $over);
        return $this->fetch();
    }

    /**
     * 节点
     * @return mixed
     */
    public function node()
    {
        $all_node = Node::where([])->count();

        $node_user = Flow::where('create_time', '>', strtotime(date('Y-m-d')))->distinct(true)->field('node_id')->select();
        $node_use = count($node_user);

        $over = $all_node - $node_use;

        $this->assign('all_node', $all_node);
        $this->assign('node_use', $node_use);
        $this->assign('over', $over);
        return $this->fetch();
    }


    /**
     * 流量
     * @return mixed
     */
    public function flow()
    {
        $sum_upload = Flow::where([])->sum('upload');
        $sum_download = Flow::where([])->sum('download');

        $upload = Flow::where('create_time', '>', strtotime(date('Y-m-d')))->sum('upload');
        $download = Flow::where('create_time', '>', strtotime(date('Y-m-d')))->sum('download');


        $this->assign('sum_upload', $sum_upload);
        $this->assign('sum_download', $sum_download);
        $this->assign('upload', $upload);
        $this->assign('download', $download);
        return $this->fetch();
    }
}
